package hongvt.quanlynhanvien;

import hongvt.quanlynhanvien.adapter.ListEmpAdapter;
import hongvt.quanlynhanvien.database.DatabaseManager;
import hongvt.quanlynhanvien.model.Employee;
import hongvt.quanlynhanvien.model.WorkingMode;
import hongvt.quanlynhanvien.utils.Define;
import hongvt.quanlynhanvien.utils.MyLog;

import java.util.List;

import com.google.gson.Gson;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import butterknife.InjectView;

public class ListEmployeeActivity extends AbstractActivity {
	@InjectView(R.id.listEmp)
	ListView listView;

	DatabaseManager dbManagement;
	List<Employee> listEmployees;
	List<WorkingMode> listWorkingModes;
	ListEmpAdapter adapter;

	@Override
	protected int getCurrentViewId() {
		// TODO Auto-generated method stub
		return R.layout.activity_list_employee;
	}

	@Override
	protected void afterInitView() {
		listView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				Intent intent = new Intent(context,
						EmployeeDetailActivity.class);
				intent.putExtra(Define.EMP_EMP_ID, listEmployees.get(position)
						.getEmpId());

				startActivity(intent);
			}
		});

		listView.setOnItemLongClickListener(new OnItemLongClickListener() {

			@Override
			public boolean onItemLongClick(AdapterView<?> arg0, View view,
					int position, long arg3) {
				showDeleteDialog(position);
				return false;
			}
		});

		dbManagement = new DatabaseManager(context);

	}

	@Override
	protected void onResume() {
		// TODO Auto-generated method stub
		super.onResume();

		listEmployees = dbManagement.getListEmployees();
		listWorkingModes = dbManagement.getListWorkingMode();

		MyLog.i("listEmployees", new Gson().toJson(listEmployees));
		MyLog.i("listWorkingModes", new Gson().toJson(listWorkingModes));

		adapter = new ListEmpAdapter(context, listEmployees, listWorkingModes);

		listView.setAdapter(adapter);
	}

	public void showDeleteDialog(final int position) {
		AlertDialog.Builder builder = new AlertDialog.Builder(context);
		builder.setTitle(R.string.delete_title);
		builder.setMessage(getString(R.string.delete_mesage) + " "
				+ listEmployees.get(position).getName() + "?");
		builder.setPositiveButton(getText(R.string.ok),
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						dbManagement.deleteEmpById(listEmployees.get(position)
								.getEmpId());
						listEmployees.remove(position);
						adapter.notifyDataSetChanged();
					}
				});

		builder.setNegativeButton(getText(R.string.cancel), null);
		builder.create().show();
	}
}
